package com.jsle.financial.manager;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jsle.financial.dao.CompanyDao;
import com.jsle.financial.domain.Company;
import com.jsle.financial.domain.Company_Resources;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Result;

/**
 * Manager 用于事物控制
 * @author zhangyanqing
 * @date 2012-10-27
 */
public class CompanyManagerImpl implements CompanyManager{
	private Log log = LogFactory.getLog(CompanyManagerImpl.class);
	
	@Resource
	private CompanyDao companyDao;
	
	/**
	 * 新增一用户
	 * @param user
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result addCompany(Company company){
		Result result = new Result();
		try {
			result = companyDao.addCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("new company error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 删除一用户
	 * @param user
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result delCompany(Company company){
		Result result = new Result();
		try {
			result = companyDao.delCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("del company error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 更新一用户信息
	 * @param user
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result updateCompany(Company company){
		Result result = new Result();
		try {
			result = companyDao.updateCompany(company);

		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_UPDATE_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_UPDATE_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("update company error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 公司配置资源
	 * @param cr
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result addCompanyAndResources(Company_Resources cr){
		Result result = new Result();
		try {
			result = companyDao.addCompanyAndResources(cr);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("new Company_Resources error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 根据公司code删除人员信息
	 * @param company
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result delUsersByCompany(Company company){
		Result result = new Result();
		try {
			result = companyDao.delUsersByCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_MESSAGE, "delUsersByCompany"));
			log.error("delUsersByCompany error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司code删除用户组信息
	 * @param company
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result delUserGroupsByCompany(Company company){
		Result result = new Result();
		try {
			result = companyDao.delUserGroupsByCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_MESSAGE, "delUserGroupsByCompany"));
			log.error("delUserGroupsByCompany error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司code删除公司关联资源信息
	 * @param company
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result delCompanyResourcesByCompany(Company company){
		Result result = new Result();
		try {
			result = companyDao.delCompanyResourcesByCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_MESSAGE, "delCompanyResourcesByCompany"));
			log.error("delCompanyResourcesByCompany error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据公司code删除人员-用户组关联信息
	 * @param company
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result delUserUserGroupsByCompany(Company company){
		Result result = new Result();
		try {
			result = companyDao.delUserUserGroupsByCompany(company);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_MESSAGE, "delUserUserGroupsByCompany"));
			log.error("delUserUserGroupsByCompany error## "+ e.getMessage());
		}
		return result;
	}
}
